package com.hskn.hss.module.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.alarmcar.entity.AlarmCar;
import com.hskn.hss.module.alarmcar.mapper.AlarmCarMapper;
import com.hskn.hss.module.alarmcar.service.IAlarmCarService;
import com.hskn.hss.module.alarmperson.entity.AlarmPerson;
import com.hskn.hss.module.alarmperson.mapper.AlarmPersonMapper;
import com.hskn.hss.module.alarmperson.service.IAlarmPersonService;
import com.hskn.hss.module.alarmsheshi.entity.AlarmSheshi;
import com.hskn.hss.module.alarmsheshi.mapper.AlarmSheshiMapper;
import com.hskn.hss.module.alarmsheshi.service.IAlarmSheshiService;
import com.hskn.hss.module.depart.entity.SysDepart;
import com.hskn.hss.module.depart.service.ISysDepartService;
import com.hskn.hss.module.statistics.vo.TlCarListRes;
import com.hskn.hss.module.statistics.vo.TlInstitutionsListRes;
import com.hskn.hss.module.statistics.vo.TlPersonListRes;
import com.hskn.hss.module.statistics.vo.TlSheshiListRes;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import com.hskn.hss.utils.excel.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报警报表
 *
 * @author hskn
 * @since 2021-05-25
 */
@Slf4j
@Controller
@RequestMapping("/hss/policereport")
public class PoliceReportController {

    @Autowired
    private IAlarmPersonService alarmPersonService;
    @Autowired
    private IAlarmCarService alarmCarService;
    @Autowired
    private IAlarmSheshiService alarmSheshiService;
    @Autowired
    private ISysDepartService sysDepartService;
    @Resource
    private AlarmPersonMapper alarmPersonMapper;
    @Resource
    private AlarmCarMapper alarmCarMapper;
    @Resource
    private AlarmSheshiMapper alarmSheshiMapper;

    /**
     * 机构报警图表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getInstitutionsPolice", method = RequestMethod.POST)
    public AjaxResult<Object> getInstitutionsPolice(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = new JSONObject();
            List<Object> legendData = new ArrayList<>();
            legendData.add("人员报警");
            legendData.add("车辆报警");
            legendData.add("设施报警");
            json.put("legendData", legendData);

            QueryWrapper<SysDepart> queryWrapperx = new QueryWrapper<>();
            queryWrapperx.isNotNull("depart_name");
            queryWrapperx.select("depart_name");
            if (params != null) {
                if (StringUtils.isNotEmpty(params.getString("departName")) && params.getString("departName") != null) {
                    queryWrapperx.lambda().eq(SysDepart::getDepartName, params.getString("departName"));
                }
            }
            List<Object> xAxisData = sysDepartService.listObjs(queryWrapperx);
            json.put("xAxisData", xAxisData);

            QueryWrapper<SysDepart> queryWrapperxy = new QueryWrapper<>();
            queryWrapperxy.select("id,depart_name");
//            queryWrapperxy.lambda().eq(SysDepart::getDepartName, "抽吸车");
            if (params != null) {
                if (StringUtils.isNotEmpty(params.getString("departName")) && params.getString("departName") != null) {
                    queryWrapperxy.lambda().eq(SysDepart::getDepartName, params.getString("departName"));
                }
            }
            List<Map<String, Object>> xAxisDatay = sysDepartService.listMaps(queryWrapperxy);
            List<Map<String, List>> seriesData = new ArrayList<Map<String, List>>();
            if (!xAxisDatay.isEmpty() && xAxisDatay != null) {
                List persionList = new ArrayList();
                List carList = new ArrayList();
                List sheshiList = new ArrayList();
                for (Map<String, Object> legendDatas : xAxisDatay) {
                    QueryWrapper<AlarmPerson> queryWrapperp = new QueryWrapper<>();
                    queryWrapperp.lambda().eq(AlarmPerson::getDepId, legendDatas.get("id"));
                    int persion = alarmPersonService.count(queryWrapperp);
                    persionList.add(persion);
                    QueryWrapper<AlarmCar> queryWrapperc = new QueryWrapper<>();
                    queryWrapperc.lambda().eq(AlarmCar::getDepId, legendDatas);
                    int car = alarmCarService.count(queryWrapperc);
                    carList.add(car);
                    QueryWrapper<AlarmSheshi> queryWrappers = new QueryWrapper<>();
                    queryWrappers.lambda().eq(AlarmSheshi::getDepId, legendDatas);
                    int sheshi = alarmSheshiService.count(queryWrappers);
                    sheshiList.add(sheshi);
                }
                Map<String, List> map = new HashMap<String, List>();
                map.put("人员报警", persionList);
                map.put("车辆报警", carList);
                map.put("设施报警", sheshiList);
                seriesData.add(map);
            }
            json.put("seriesData", seriesData);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 机构报警汇总
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getInstitutionsPoliceList", method = RequestMethod.POST)
    public AjaxResult<Object> getInstitutionsPoliceList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = sysDepartService.collectList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 机构报警导出
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getInstitutionsPoliceExport", method = RequestMethod.POST)
    public AjaxResult<Object> getInstitutionsPoliceExport(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = new JSONObject();
            QueryWrapper<SysDepart> queryWrapperxy = new QueryWrapper<>();
            queryWrapperxy.select("id,depart_name,org_type");
            if (params != null) {
                if (StringUtils.isNotEmpty(params.getString("departName")) && params.getString("departName") != null) {
                    queryWrapperxy.lambda().eq(SysDepart::getDepartName, params.getString("departName"));
                }
            }
            List<Map<String, Object>> xAxisDatay = sysDepartService.listMaps(queryWrapperxy);

            List<TlInstitutionsListRes> institutionsList = new ArrayList<TlInstitutionsListRes>();
            if (!xAxisDatay.isEmpty() && xAxisDatay != null) {
                for (Map<String, Object> legendDatas : xAxisDatay) {
                    QueryWrapper<AlarmPerson> queryWrapperp = new QueryWrapper<>();
                    queryWrapperp.lambda().eq(AlarmPerson::getDepId, legendDatas.get("id"));
                    int persion = alarmPersonService.count(queryWrapperp);
                    QueryWrapper<AlarmCar> queryWrapperc = new QueryWrapper<>();
                    queryWrapperc.lambda().eq(AlarmCar::getDepId, legendDatas);
                    int car = alarmCarService.count(queryWrapperc);
                    QueryWrapper<AlarmSheshi> queryWrappers = new QueryWrapper<>();
                    queryWrappers.lambda().eq(AlarmSheshi::getDepId, legendDatas);
                    int sheshi = alarmSheshiService.count(queryWrappers);

                    TlInstitutionsListRes institutions = new TlInstitutionsListRes();
                    institutions.setDepartName(legendDatas.get("depart_name").toString());
                    institutions.setOrgType(legendDatas.get("org_type").toString());
                    institutions.setCountPolice(persion + car + sheshi);
                    institutions.setPerson(persion);
                    institutions.setCar(car);
                    institutions.setSheshi(sheshi);
                    institutionsList.add(institutions);
                }
            }
            ExcelUtil.exportExcel(institutionsList, "机构报警.xlsx", "机构报警", "sheet1", TlInstitutionsListRes.class);
            json.put("institutionsList", institutionsList);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 人员报警图表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getPersonnelPolice", method = RequestMethod.POST)
    public AjaxResult<Object> getPersonnelPolice(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = alarmPersonService.getPersonnelPolice(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 人员报警汇总
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getPersonnelPoliceList", method = RequestMethod.POST)
    public AjaxResult<Object> getPersonnelPoliceList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = alarmPersonService.collectList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 人员报警汇总导出
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getPersonPoliceExport", method = RequestMethod.POST)
    public AjaxResult<Object> getPersonPoliceExport(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = new JSONObject();
            List<TlPersonListRes> personList = alarmPersonMapper.collectListExport(params);
            ExcelUtil.exportExcel(personList, "人员报警汇总.xlsx", "人员报警汇总", "sheet1", TlPersonListRes.class);
            json.put("personList", personList);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 人员报警明细
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getPersonnelPoliceDetail", method = RequestMethod.POST)
    public AjaxResult<Object> getPersonnelPoliceDetail(HttpServletRequest request) {
        try {
            BaseEntity entity = new BaseEntity();
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            alarmPersonService.mList(entity, params);
            return AjaxResult.success("处理成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 车辆报警图表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getCarPolice", method = RequestMethod.POST)
    public AjaxResult<Object> getCarPolice(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = alarmCarService.getCarPolice(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 车辆报警汇总
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getCarPoliceList", method = RequestMethod.POST)
    public AjaxResult<Object> getCarPoliceList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = alarmCarService.collectList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 车辆报警汇总导出
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getCarPoliceExport", method = RequestMethod.POST)
    public AjaxResult<Object> getCarPoliceExport(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = new JSONObject();
            List<TlCarListRes> carList = alarmCarMapper.collectListExport(params);
            ExcelUtil.exportExcel(carList, "车辆报警汇总.xlsx", "车辆报警汇总", "sheet1", TlCarListRes.class);
            json.put("personList", carList);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 车辆报警明细
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getCarPoliceDetail", method = RequestMethod.POST)
    public AjaxResult<Object> getCarPoliceDetail(HttpServletRequest request) {
        try {
            BaseEntity entity = new BaseEntity();
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            alarmCarService.mList(entity, params);
            return AjaxResult.success("处理成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 设施报警图表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getFacilitiesPolice", method = RequestMethod.POST)
    public AjaxResult<Object> getFacilitiesPolice(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = alarmSheshiService.getSheshiPolice(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 设施报警汇总
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getSheshiPoliceList", method = RequestMethod.POST)
    public AjaxResult<Object> getSheshiPoliceList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = alarmSheshiService.collectList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 设施报警汇总导出
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getSheshiPoliceExport", method = RequestMethod.POST)
    public AjaxResult<Object> getSheshiPoliceExport(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = new JSONObject();
            List<TlSheshiListRes> sheshiList = alarmSheshiMapper.collectListExport(params);
            ExcelUtil.exportExcel(sheshiList, "设施报警汇总.xlsx", "设施报警汇总", "sheet1", TlSheshiListRes.class);
            json.put("personList", sheshiList);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 设施报警明细
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getPersonnelSheshiPoliceDetail", method = RequestMethod.POST)
    public AjaxResult<Object> getPersonnelSheshiPoliceDetail(HttpServletRequest request) {
        try {
            BaseEntity entity = new BaseEntity();
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            alarmSheshiService.mList(entity, params);
            return AjaxResult.success("处理成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }
}
